package com.sx.manage.controller.Product;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.product.IMOperatorFlowService;
import com.sx.manage.service.sys.IMBaseSystemConfigInfoService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.manage.util.ExcelUtils;
import com.sx.manage.util.PropertiesUtils;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.manage.mybatis.form.product.MOperatorFlowForm;
import com.sx.model.manage.mybatis.form.sys.MSystemConfigInfoForm;
import com.sx.model.manage.mybatis.vo.product.MFlowRechargeVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultExcelVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.manage.mybatis.vo.sys.MSysConfigVO;

@Controller
@RequestMapping("/operator_flow_info")
@SuppressWarnings({"rawtypes","unchecked"})
public class MOperatorFlowController extends MBaseController {

	private Logger log = LoggerFactory.getLogger(this.getClass());
	@Resource(name = "operatorFlowServiceImpl")
	private IMOperatorFlowService operatorFlowServiceImpl;
	/** 系统参数配置 **/
	@Resource(name = "mBaseSystemConfigInfoServiceImpl")
	private IMBaseSystemConfigInfoService systemConfigInfoServiceImpl;
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "operator_flow_view_index")
	public ViewAndModel add_viewOperatorFlowInfo() {
		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("productsMgr/flowRecharge/flowList");// 修改ftl页面名称

		return model;
	}

	@RequestMapping("/operator_flow_page")
	@ResponseBody
	public ResultListVO<MFlowRechargeVO> searchProductCallChargeInfo(MOperatorFlowForm form) {
		return this.operatorFlowServiceImpl.queryOperatorFlow(form);
	}


	@RequestMapping("/operator_flow_import")
	@ResponseBody
	public ResultListVO<MFlowRechargeVO> import_operatorFlowInfo(@RequestParam(value = "files", required = false) MultipartFile file) {
		if (null == file) {
			return new ResultListVO<>("sj.m.file.info.error");
		}
		String fileName = file.getOriginalFilename();
		log.info(" 公司用户=====导入文件名称为:" + fileName);
		String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
		if (!"xlsx".equals(prefix)) {
			return new ResultListVO<>(false, "只支持2007版Excel!", null);
		}
		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_name(MSystemConstants.CALL_FLOW_PRODUCT_ID);
		ResultObjectVO<MSysConfigVO> configResult = systemConfigInfoServiceImpl.queryByIdSystemConfig(form);
		if (null == configResult.getObjVo()||null==configResult.getObjVo().getId()) {
			log.info("系统配置配置话费对用的产品id请配置{}",MSystemConstants.CALL_FLOW_PRODUCT_ID);
			return new ResultListVO<>("sx.call.charge.product.id.is.null");
		}
		List<Map<String, String>> listMap = PropertiesUtils.proToMaps(MSystemConstants.PRODUCTFLOW_FILE_PATH);
		ResultExcelVO results = ExcelUtils.fileToExcel(file, listMap.get(0), listMap.get(1));
		if (!results.isSuccess()) {
			return new ResultListVO<>(false, null, results.getMsg());
		}
		return operatorFlowServiceImpl.importOperatorFlow(results.getListMap(),configResult.getObjVo());
	}

	@RequestMapping("/callcharge_byid/{id}")
	@ResponseBody
	public ViewAndModel queryBy_viewOperatorFlowInfo(MOperatorFlowForm form) {
		ViewAndModel model = new ViewAndModel(request, cache);
		// 查询类
		ResultObjectVO<MFlowRechargeVO> result = operatorFlowServiceImpl.queryOperatorFlowById(form);
		model.addObject("mOperatorFlowInfoVO", result.getObjVo());
		model.setViewName("productsMgr/flowRecharge/editFlow");// 修改ftl页面名称
		return model;
	}

	@RequestMapping(value = "operator_flow_eidt")
	@ResponseBody
	public ResultBooleanVO add_operatorFlowInfo(HttpServletRequest request, HttpServletResponse response) {
		if (null == request) {
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		String requestMessage = extract_param_from_request(request);
		if (null == requestMessage || "".equals(requestMessage)) {
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		MOperatorFlowForm form = JackJsonUtil.jsonToJavaBean(requestMessage, MOperatorFlowForm.class);
		if (null == form) {
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		if (0 == form.getId()) {
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		return this.operatorFlowServiceImpl.editOperatorFlow(form, loginConsumer());
	}

	/**
	 * [ProductCallChargeInfo]停用启用 流程
	 * 
	 * @return productSave
	 */
	@RequestMapping("/callcharge_edit")
	@ResponseBody
	public ResultBooleanVO changeCallChargeStatus(HttpServletRequest request, HttpServletResponse response) {
		String requestMessage = extract_param_from_request(request);
		if (null == requestMessage) {
			return new ResultBooleanVO(false, "设置条件错误", null);
		}
		log.info("requestMessage:" + requestMessage);

		return operatorFlowServiceImpl.changeOperatorFlowStatus(requestMessage);
	}

}
